set nocompatible
source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/mswin.vim
behave mswin

set diffexpr=MyDiff()
function MyDiff()
  let opt = '-a --binary '
  if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
  if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
  let arg1 = v:fname_in
  if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
  let arg2 = v:fname_new
  if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
  let arg3 = v:fname_out
  if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
  let eq = ''
  if $VIMRUNTIME =~ ' '
    if &sh =~ '\<cmd'
      let cmd = '""' . $VIMRUNTIME . '\diff"'
      let eq = '"'
    else
      let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
    endif
  else
    let cmd = $VIMRUNTIME . '\diff'
  endif
  silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3 . eq
endfunction

colo morning
set guifont=consolas:h10:w6
set nu



" comment line.
autocmd FileType c,cpp map tm :s/^/\/\/\t/g<CR>:nohlsearch<CR>
" uncomment line.
autocmd FileType c,cpp map tu :s/^\t*\s*\/\/\t*\s*/\t/<CR>
" replace current word.
vmap tr "xy:exe "%s/" . @x . "/" . input("replace [" . @x . "] by: ") . "/cg"<CR>

" find word in directories.
function SearchWordGlobal()
	let cw ="/" . input("global search: ", expand("<cword>")) . "/g" 
	let distPath = input("search in: ",  expand("%:h") . "./../**/*")
	exe "vimgrep " . cw . " " . distPath . ""
	exe ":cw"
endfunction
nmap <silent> tgf :call SearchWordGlobal()<CR>
" hex model
nnoremap <silent> hex  :%!xxd<CR>
nnoremap <silent> nhex  :%!xxd -r<CR>

" quick fixs
map <silent> tcn :cn<CR>
map <silent> tcp :cp<CR>
map <silent> tco :copen<CR>
map <silent> tcl :cclose<CR>
" buffers switch, switching
map <silent> bn :bn<CR>
map <silent> bp :bp<CR>
map <silent> bd :bd<CR>
map <silent> bs :buffers<CR>:exe "buf " . input("switch to :") . ""<CR>

" add file discription.
function AddDescription()

	let lineCnt = len(getbufline(bufname("%"), 0, "$"))
	if lineCnt > 1
		return 0
	endif
	let time ="* @" .  strftime("%Y-%m-%d %T", localtime()) 
	let file = "*" . expand("%")
	let author = "* created by yuhui."
	let description = ['/******************', file, "*", "*", author, time, '*******************/']
	let exfn = tolower(strpart(file, strlen(file)-2, strlen(file)))
	if  exfn == ".h"
		let macro = toupper(expand("%"))
		let macro = "#ifndef " . strpart(macro, 0, strlen(macro)-2) . "_H"
		echo macro
		call add(description, macro)
		echo description
		let macro = "#define " . strpart(macro, 0, strlen(macro)-2) . "_H"
		call add(description, macro)
		call add(description, " ")
		call add(description, "{")
		call add(description, "}")
		call add(description, "#endif")
	endif
	let fail= append(0, description)
	if fail
		return 1
	endif
endfunction
autocmd FileType c,cpp,h exe ":call AddDescription()"
"for GUI
set guioptions-=T

"for foldding
set foldcolumn=2
"set foldmethod=indent
"let &termencoding=&encoding
"set fileencodings=utf-8		",gbk,ucs-bom,cp936
"set termencoding=utf-8,gbk,ucs-bom
"set encoding=utf-8
set laststatus=2
set statusline=%<%F\ %ybuf:%n%h%m%r%=%-14.(%l,%c%V%)\ %B@%O\ %r%P\  
""""""""""""""""""""""""""""""for NERDTree
" >> auto change current directory to current openning file.
let nerdTreeRoot = "d:"
nnoremap <silent> <F2> :let curPath =expand("%:h:p")<Bar> exe "NERDTree " . (len(curPath)<1 ?
			\nerdTreeRoot : curPath) . ""<CR>exe ":! cd " . (len(curPath)<1 ?
			\nerdTreeRoot : curPath) . ""<CR>

""""""""""""""""""""""""""""""""""""""""""" ctags
let Tlist_Process_File_Always = 1
let Tlist_WinWidth = 50
let Tlist_Auto_Open = 0
let Tlist_Auto_Update = 1

let Tlist_Exit_OnlyWindow = 1
let Tlist_GainFocus_on_ToggleOpen = 1
let Tlist_Highlight_Tag_On_BufEnter = 1
let Tlist_Auto_Highlight_Tag = 1
let Tlist_Close_On_Select = 1
let Tlist_Use_SingleClick = 1
autocmd FileType c,cpp,java,python nnoremap <silent> T :TlistToggle<CR>:winc w<CR>

"""""""""""""""""""""""""""""""""""
"open current file's header or cpp. toc : t mode o open c corespond.
function OpenCorrespondFile()
	let fname=expand("%")
	let idot=strridx(fname, ".")
	echo fname ", " idot
	if idot >0 
		let exh =tolower(strpart(fname, idot+1, 1))
		let cospf = "vs " . strpart(fname, 0, idot) . "." . ( exh == "h" ? "cpp" : "h")
		exe cospf
	else
		echo fname . "is not a valid source file"
	endif
endfunction
autocmd FileType c,cpp nnoremap <silent> toc : call OpenCorrespondFile()<CR>

